/**
 * @author   zhangyp
 * @created  2013-11-10 上午11:25:04
 * @email    zviolet@163.com
 * TODO
 */
package com.bible.fire.util.file;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.bible.fire.R;
import com.bible.fire.util.SystemConstant;
import com.bible.fire.util.ToastUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FileBrowseHelper {
	
	//private static String rootPath = "/sdcard/";
	private static String rootPath = SystemConstant.SDCARD_ROOT_PATH;
	
	static{
		if( !rootPath.endsWith("/") ){
			rootPath += "/";
		}
	}
	
	private static void getFileDir(Context context, String filePath, TextView mPath, ListView listView, boolean filterFile ){  
		  
        File f=new File(filePath);      
        if( f.exists() && f.canWrite() ){  
            mPath.setText(filePath);  
            final List<String> items = new ArrayList<String>();  
    	    final List<String> paths = new ArrayList<String>();  
            File[] files=f.listFiles();  
            if(!filePath.equals(rootPath) && !rootPath.equals( filePath + "/") ){  
                items.add("goparent");  
                paths.add(f.getParent());  
            }  
            for(int i=0;i<files.length;i++){  
                File file=files[i];  
                if(file.isDirectory() || !filterFile ){  
                    items.add(file.getName());  
                    paths.add(file.getPath());  
                }
            }  
            FileBrowseAdapter browseAdapter = new FileBrowseAdapter( context, items, paths );  
            listView.setAdapter( browseAdapter );
            
            final Context fcontext = context;
            final TextView tv = mPath;
            final ListView fileList = listView;
            final boolean isFilter = filterFile;
            
            listView.setOnItemClickListener(new OnItemClickListener(){                   
                @Override  
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {                      
                    if( items.get(position).equals("goparent") ){  
                        getFileDir(fcontext, paths.get(position), tv, fileList, isFilter );  
                    }else{  
                        File file=new File( paths.get(position) );
                        if(file.canWrite()){  
                            if (file.isDirectory()){  
                                getFileDir( fcontext, paths.get(position), tv, fileList, isFilter );  
                            }else{
                            	tv.setText( paths.get(position) );
                            }
                        }else{  
                            ToastUtil.showMsg( fcontext, "很抱歉您的权限不足!");
                        }  
                    }  
                }  
            });  
        }else{
        	ToastUtil.showMsg( context, "无SD卡");   
        } 
	}
	
	private static void addBrowseView( Context context, ViewGroup container, boolean filterFile, String fileDir ){
		LayoutInflater mInflater = LayoutInflater.from( context );
		View view = mInflater.inflate(R.layout.file_browse, null);
		container.addView( view );
		
		TextView mPath = (TextView)view.findViewById( R.id.mPath );
		mPath.setTextColor( context.getResources().getColor(R.color.text_color) );
		View rootBtn = view.findViewById( R.id.rootdirBtn );
		ListView listView = (ListView)view.findViewById(R.id.filelist);
		
		if( fileDir == null || fileDir.trim().length() == 0 ){
			fileDir = rootPath;
		}		
		final Context fcontext = context;
        final TextView tv = mPath;
        final ListView fileList = listView;
        final boolean isFilter = filterFile;
        
		rootBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				getFileDir( fcontext, rootPath, tv, fileList, isFilter );	
			}
		});
		getFileDir( fcontext, fileDir, tv, fileList, isFilter );
	}
	
	public static void addFileBrowseView( Activity context, ViewGroup container, String fileDir ){
		addBrowseView( context, container, false, fileDir );
	}
	
    public static void addDirBrowseView( Activity context, ViewGroup container, String fileDir ){
    	addBrowseView( context, container, true, fileDir );
	}

}
